//
//  SISSHousingProjectViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/9/14.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSHousingProjectViewController.h"
#import "SISSHousingProjectFirstView.h"
#import "SISSHousingProjectInfoView.h"
#import "SISSSpecialOffersView.h"
#import "SISSHouseProjectPriceTrendView.h"
#import "SISSChatViewController.h"
#import "SISSJsonRequestNetworkingManager+Resources.h"

@interface SISSHousingProjectViewController ()<SDCycleScrollViewDelegate,SISSHousingProjectFirstViewDelegate>

/// 图片轮播
@property (weak, nonatomic) SDCycleScrollView *cycleScrollView;
/// 图片分页
@property (weak, nonatomic) UILabel *pageLabel;

///
@property (weak, nonatomic) SISSHousingProjectFirstView *projectFirstView;
/// info
@property (weak, nonatomic) SISSHousingProjectInfoView *projectInfoView;
/// 优惠信息
@property (weak, nonatomic) SISSSpecialOffersView *specialOffersView;

@property (weak, nonatomic) SISSHouseProjectPriceTrendView *priceTrendView;
/// 电话按钮
@property (weak, nonatomic) UIButton *telephoneButton;
/// 在线咨询按钮
@property (weak, nonatomic) UIButton *telShowButton;

/// 图片链接数组 
@property (strong, nonatomic) NSArray *imagesURLStrings;

///
@property (strong, nonatomic) SISSBuildingProjectModel *buildingProjectModel;

@end

@implementation SISSHousingProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"楼盘";
}

- (void)setBuilingName:(NSString *)builingName {
    _builingName = builingName;
    [kJsonRequestNetworkingManager requestNewHouseBuildingDetailResourceWithBuidName:builingName success:^(SISSBuildingProjectModel *buildingProjectModel) {
        self.buildingProjectModel = buildingProjectModel;
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)setBuildingProjectModel:(SISSBuildingProjectModel *)buildingProjectModel {
    _buildingProjectModel = buildingProjectModel;
    
    NSMutableArray *picArrM = [NSMutableArray array];
    for (SISSBuildPicModel *picModel in buildingProjectModel.buildPicList) {
        [picArrM addObject:picModel.buildPicUrl];
    }
    self.imagesURLStrings = picArrM.copy;
    self.cycleScrollView.imageURLStringsGroup = self.imagesURLStrings;
    self.pageLabel.text = [NSString stringWithFormat:@" %@/%@ ",@(1),@(self.imagesURLStrings.count)];
    
    self.projectFirstView.buildingName = buildingProjectModel.build.pname;
    self.projectFirstView.propertyType = buildingProjectModel.build.htype;
    self.projectFirstView.buildingAddress = buildingProjectModel.build.buildingAddress;
    
    self.projectInfoView.buildInfoModel = buildingProjectModel.build;
    
    self.specialOffersView.specialOffers = buildingProjectModel.build.voucherContext;
    
    [self.telShowButton setTitle:[NSString stringWithFormat:@"销售热线：%@",buildingProjectModel.build.tel] forState:UIControlStateNormal];
    
    self.priceTrendView.bpcleList = buildingProjectModel.bpcleList;
    
    if (buildingProjectModel.bpcleList.count == 0) {
        WeakSelf
        // 当没有图表数据时隐藏图表视图
        self.priceTrendView.hidden = YES;
        // SISSHouseProjectPriceTrendView赋值时配合数据进行隐藏
        [self.priceTrendView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.specialOffersView.mas_bottom).offset(0.5 * globelMargin);
            make.left.equalTo(weakSelf.specialOffersView);
            make.right.equalTo(weakSelf.containerView);
            make.height.mas_equalTo(10);
            make.bottom.equalTo(weakSelf.containerView).offset(-64);
        }];
    }
}

#pragma mark - delegate
#pragma mark -- SISSHousingProjectFirstViewDelegate
- (void)locationButtonClickAction {
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - 电话咨询
- (void)telephoneButtonClickAction {
    if (self.buildingProjectModel.build.tel.length > 0) {
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",self.buildingProjectModel.build.tel]]]];
        [[UIApplication sharedApplication].keyWindow addSubview:callWebview];
    }
}



#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"图片加载占位符"]];
    cycleScrollView.autoScroll = NO;
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    cycleScrollView.itemDidScrollOperationBlock = ^(NSInteger currentIndex) {
        
        self.pageLabel.text = [NSString stringWithFormat:@" %@/%@ ",@(currentIndex + 1),@(self.imagesURLStrings.count)];
    };
    [self.containerView addSubview:cycleScrollView];
    self.cycleScrollView = cycleScrollView;

    UILabel *pageLabel = [[UILabel alloc] init];
    pageLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.5];
    pageLabel.textColor = [UIColor whiteColor];
    pageLabel.text = [NSString stringWithFormat:@" %@/%@ ",@(1),@(self.imagesURLStrings.count)];
    pageLabel.textAlignment = NSTextAlignmentCenter;
    pageLabel.layer.cornerRadius = 10;
    pageLabel.layer.masksToBounds = YES;
    [cycleScrollView addSubview:pageLabel];
    self.pageLabel = pageLabel;
    
    SISSHousingProjectFirstView *projectFirstView = [[SISSHousingProjectFirstView alloc] init];
    projectFirstView.delegate = self;
    [self.containerView addSubview:projectFirstView];
    self.projectFirstView = projectFirstView;
    
    SISSHousingProjectInfoView *projectInfoView = [[SISSHousingProjectInfoView alloc] init];
    [self.containerView addSubview:projectInfoView];
    self.projectInfoView = projectInfoView;
    
    SISSSpecialOffersView *specialOffersView = [[SISSSpecialOffersView alloc] init];
    [self.containerView addSubview:specialOffersView];
    self.specialOffersView = specialOffersView;
    
    SISSHouseProjectPriceTrendView *priceTrendView = [[SISSHouseProjectPriceTrendView alloc] init];
    [self.containerView addSubview:priceTrendView];
    self.priceTrendView = priceTrendView;
    
    UIButton *telShowButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [telShowButton setTitle:@"销售热线：" forState:UIControlStateNormal];
    [telShowButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [telShowButton setBackgroundColor:MAIN_COLOR];
    telShowButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:telShowButton];
    self.telShowButton = telShowButton;
    
    UIButton *telButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [telButton setTitle:@"电话咨询" forState:UIControlStateNormal];
    [telButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [telButton setBackgroundColor:COLOR(0xC7DE21)];
    telButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [telButton addTarget:self action:@selector(telephoneButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:telButton];
    self.telephoneButton = telButton;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    CGFloat bottomViewHeight = 44.0;
    WeakSelf
    [self.cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView).offset(margin);
        if (weakSelf.imagesURLStrings.count > 0) {
            make.height.mas_equalTo(20 * margin);
        } else {
            make.height.mas_equalTo(20 * margin);
//            make.height.mas_equalTo(0);
        }
    }];
    
    [self.pageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.cycleScrollView).offset(-2 * margin);
        make.width.mas_equalTo(6 * margin);
        make.bottom.equalTo(weakSelf.cycleScrollView).offset(-margin);
    }];
    
    [self.projectFirstView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.cycleScrollView.mas_bottom);
    }];
    
    [self.projectInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.projectFirstView.mas_bottom).offset(0.2 * margin);
    }];
    
    [self.specialOffersView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.projectInfoView.mas_bottom).offset(0.5 * margin);
        make.left.right.equalTo(weakSelf.projectInfoView);
    }];
    
    [self.priceTrendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.specialOffersView.mas_bottom).offset(0.5 * margin);
        make.left.equalTo(weakSelf.specialOffersView);
        make.right.equalTo(weakSelf.containerView);
        make.bottom.equalTo(weakSelf.containerView).offset(-64);
    }];
    
    [self.telShowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view.mas_centerX).multipliedBy(1.25);
        make.height.mas_equalTo(bottomViewHeight);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    
    [self.telephoneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view);
        make.left.equalTo(weakSelf.telShowButton.mas_right);
        make.bottom.height.equalTo(weakSelf.telShowButton);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
